/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.view;

import com.singularsys.jep.EvaluationException;
import com.singularsys.jep.Jep;
import com.singularsys.jep.ParseException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dss.gil.service.ValidationService;
import org.dss.gil.util.Globals;

 /*
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */
public class Calculator extends javax.swing.JFrame {

    private static Calculator instance;
    private ValidationService validation;
    private ArrayList<Character> characters;
    private int ESC_CODE = 27;
    
    public static Calculator getInstance(){
        if(instance==null){
            instance = new Calculator();
        }
        return instance;
    }
    
    private Calculator() {
        initComponents();
        setIconImage(Globals.logoImg);
        setLabels();
        initValidChars();
        validation = ValidationService.getInstance();
    }

    
    public void setLabels(){
        setTitle(Globals.language.getMessage("title.calc"));
    }
    
    private void initValidChars(){
        characters = new ArrayList<Character>();
        characters.add('0');
        characters.add('1');
        characters.add('2');
        characters.add('3');
        characters.add('4');
        characters.add('5');
        characters.add('6');
        characters.add('7');
        characters.add('8');
        characters.add('9');
        characters.add('(');
        characters.add(')');
        characters.add('/');
        characters.add('*');
        characters.add('%');
        characters.add('+');
        characters.add('-');
        characters.add('^');
        characters.add('.');
        characters.add(' ');
    }
    
    
/*********************************************************************
************************* C A L C U L A T O R ************************
*********************************************************************/  
    
    private void removeInvalidChars() {
        String text = "";
        int position = jTextOperation.getCaretPosition();
        for( char c : jTextOperation.getText().toCharArray()){
            if(characters.contains(c)){
                text+=c;
            }else{
                position--;
            }
        }
        position=(position>text.length())?text.length():position;
        jTextOperation.setText(text);
        jTextOperation.setCaretPosition(position);
    }
    
    private void addValue(String val){
        jTextOperation.setText(jTextOperation.getText()+val);
    }
    
    private String calculate(String expresion){
        try {
            Jep j = new Jep();
            j.parse(expresion);
            return (j.evaluate().toString());
        } catch (EvaluationException ex) {
            Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, ex.getMessage());
        } catch (ParseException ex) {
            Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        return "Error";
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jTextOperation = new javax.swing.JTextField();
        jTextResult = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        jButOpenBrack = new javax.swing.JButton();
        jButCloseBrack = new javax.swing.JButton();
        jButPower = new javax.swing.JButton();
        jButDiv = new javax.swing.JButton();
        jBut7 = new javax.swing.JButton();
        jBut8 = new javax.swing.JButton();
        jBut9 = new javax.swing.JButton();
        jButMult = new javax.swing.JButton();
        jBut4 = new javax.swing.JButton();
        jBut5 = new javax.swing.JButton();
        jBut6 = new javax.swing.JButton();
        jButMod = new javax.swing.JButton();
        jBut1 = new javax.swing.JButton();
        jBut2 = new javax.swing.JButton();
        jBut3 = new javax.swing.JButton();
        jButPlus = new javax.swing.JButton();
        jBut0 = new javax.swing.JButton();
        jButDot = new javax.swing.JButton();
        jButResult = new javax.swing.JButton();
        jButMinus = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jTextDouble = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextTime = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setResizable(false);

        jTextOperation.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextOperation.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextOperationKeyReleased(evt);
            }
        });

        jTextResult.setEditable(false);
        jTextResult.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextResult.setFocusable(false);

        jPanel2.setLayout(new java.awt.GridLayout(5, 4));

        jButOpenBrack.setText("(");
        jButOpenBrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButOpenBrackActionPerformed(evt);
            }
        });
        jPanel2.add(jButOpenBrack);

        jButCloseBrack.setText(")");
        jButCloseBrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButCloseBrackActionPerformed(evt);
            }
        });
        jPanel2.add(jButCloseBrack);

        jButPower.setText("^");
        jButPower.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButPowerActionPerformed(evt);
            }
        });
        jPanel2.add(jButPower);

        jButDiv.setText("/");
        jButDiv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButDivActionPerformed(evt);
            }
        });
        jPanel2.add(jButDiv);

        jBut7.setText("7");
        jBut7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut7ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut7);

        jBut8.setText("8");
        jBut8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut8ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut8);

        jBut9.setText("9");
        jBut9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut9ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut9);

        jButMult.setText("*");
        jButMult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButMultActionPerformed(evt);
            }
        });
        jPanel2.add(jButMult);

        jBut4.setText("4");
        jBut4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut4ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut4);

        jBut5.setText("5");
        jBut5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut5ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut5);

        jBut6.setText("6");
        jBut6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut6ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut6);

        jButMod.setText("%");
        jButMod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButModActionPerformed(evt);
            }
        });
        jPanel2.add(jButMod);

        jBut1.setMnemonic('1');
        jBut1.setText("1");
        jBut1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut1ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut1);

        jBut2.setText("2");
        jBut2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut2ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut2);

        jBut3.setText("3");
        jBut3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut3ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut3);

        jButPlus.setText("+");
        jButPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButPlusActionPerformed(evt);
            }
        });
        jPanel2.add(jButPlus);

        jBut0.setText("0");
        jBut0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut0ActionPerformed(evt);
            }
        });
        jPanel2.add(jBut0);

        jButDot.setText(".");
        jButDot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButDotActionPerformed(evt);
            }
        });
        jPanel2.add(jButDot);

        jButResult.setText("=");
        jButResult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButResultActionPerformed(evt);
            }
        });
        jPanel2.add(jButResult);

        jButMinus.setText("-");
        jButMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButMinusActionPerformed(evt);
            }
        });
        jPanel2.add(jButMinus);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextOperation, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(jTextResult, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextOperation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/calc_x24.png")), jPanel1); // NOI18N

        jLabel1.setText("###.##, ...");

        jLabel2.setText("HH:MM:SS, ...");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/down_x24.png"))); // NOI18N
        jButton1.setToolTipText("HH:MM:SS -> ###.##");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/up_x24.png"))); // NOI18N
        jButton2.setToolTipText("###.## -> HH:MM:SS ");
        jButton2.setSize(new java.awt.Dimension(40, 36));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTextDouble, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jTextTime, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(33, 33, 33))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextDouble, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(114, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/org/dss/gil/view/img/clock_x24.png")), jPanel3); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButResultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButResultActionPerformed
        jTextResult.setText(calculate(jTextOperation.getText()));
}//GEN-LAST:event_jButResultActionPerformed

    private void jTextOperationKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextOperationKeyReleased
        if(evt.getKeyCode()==ESC_CODE){
            jTextOperation.setText("");
            jTextResult.setText("");
        }else if(evt.isActionKey() || evt.isShiftDown() || evt.isAltDown() || evt.isControlDown()){
        }else{
            removeInvalidChars();
        }
    }//GEN-LAST:event_jTextOperationKeyReleased

    private void jButOpenBrackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButOpenBrackActionPerformed
        addValue(jButOpenBrack.getText());
    }//GEN-LAST:event_jButOpenBrackActionPerformed

    private void jButCloseBrackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButCloseBrackActionPerformed
        addValue(jButCloseBrack.getText());
    }//GEN-LAST:event_jButCloseBrackActionPerformed

    private void jButPowerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButPowerActionPerformed
        addValue(jButPower.getText());
    }//GEN-LAST:event_jButPowerActionPerformed

    private void jButDivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButDivActionPerformed
        addValue(jButDiv.getText());
    }//GEN-LAST:event_jButDivActionPerformed

    private void jBut7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut7ActionPerformed
        addValue(jBut7.getText());
    }//GEN-LAST:event_jBut7ActionPerformed

    private void jBut8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut8ActionPerformed
        addValue(jBut8.getText());
    }//GEN-LAST:event_jBut8ActionPerformed

    private void jBut9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut9ActionPerformed
        addValue(jBut9.getText());
    }//GEN-LAST:event_jBut9ActionPerformed

    private void jButMultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButMultActionPerformed
        addValue(jButMult.getText());
    }//GEN-LAST:event_jButMultActionPerformed

    private void jBut4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut4ActionPerformed
        addValue(jBut4.getText());
    }//GEN-LAST:event_jBut4ActionPerformed

    private void jBut5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut5ActionPerformed
        addValue(jBut5.getText());
    }//GEN-LAST:event_jBut5ActionPerformed

    private void jBut6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut6ActionPerformed
        addValue(jBut6.getText());
    }//GEN-LAST:event_jBut6ActionPerformed

    private void jButModActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButModActionPerformed
        addValue(jButMod.getText());
    }//GEN-LAST:event_jButModActionPerformed

    private void jBut1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut1ActionPerformed
        addValue(jBut1.getText());
    }//GEN-LAST:event_jBut1ActionPerformed

    private void jBut2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut2ActionPerformed
        addValue(jBut2.getText());
    }//GEN-LAST:event_jBut2ActionPerformed

    private void jBut3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut3ActionPerformed
        addValue(jBut3.getText());
    }//GEN-LAST:event_jBut3ActionPerformed

    private void jButPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButPlusActionPerformed
        addValue(jButPlus.getText());
    }//GEN-LAST:event_jButPlusActionPerformed

    private void jBut0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut0ActionPerformed
        addValue(jBut0.getText());
    }//GEN-LAST:event_jBut0ActionPerformed

    private void jButDotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButDotActionPerformed
        addValue(jButDot.getText());
    }//GEN-LAST:event_jButDotActionPerformed

    private void jButMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButMinusActionPerformed
        addValue(jButMinus.getText());
    }//GEN-LAST:event_jButMinusActionPerformed

    
/*********************************************************************
*************************  TIME  PARSER  *************************
*********************************************************************/    
    
    private String timeToDouble(String time){
        String result ="";
        for(String token : time.split(",")){
           if(validation.isHH_MM_SS(token)){
               result+= toDouble(token);
           }
           result+=", ";
        }
        return result;
    }
    
    private String doubleToTime(String value){
        String result ="";
        for(String token : value.split(",")){
           if(validation.isDouble(token)){
               result+= toHMS(new Double(token.trim()));
           }
           result+=", ";
        }
        return result;
    }
    
    public String toHMS(double val){
        int hr = (int) (val/60);
        int mn = (int) (val-(hr*60));
        int sc = (int) Math.round((val-(hr*60)-mn)*60);
        if(sc==60)sc--;
        return (hr+":"+mn+":"+sc);
    }
    
    public String toDouble(String time){
        String[] values = time.split(":");
        return (Globals.decimalFormat.format(new Double(values[0])*60) + new Double(values[1]) + (new Double(values[2])/60)) +"";
    }
    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if(!jTextDouble.getText().trim().isEmpty()){
        jTextTime.setText( doubleToTime(jTextDouble.getText().trim()) );
    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    if(!jTextTime.getText().trim().isEmpty()){
        jTextDouble.setText( timeToDouble(jTextTime.getText().trim()) );
    }
}//GEN-LAST:event_jButton2ActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBut0;
    private javax.swing.JButton jBut1;
    private javax.swing.JButton jBut2;
    private javax.swing.JButton jBut3;
    private javax.swing.JButton jBut4;
    private javax.swing.JButton jBut5;
    private javax.swing.JButton jBut6;
    private javax.swing.JButton jBut7;
    private javax.swing.JButton jBut8;
    private javax.swing.JButton jBut9;
    private javax.swing.JButton jButCloseBrack;
    private javax.swing.JButton jButDiv;
    private javax.swing.JButton jButDot;
    private javax.swing.JButton jButMinus;
    private javax.swing.JButton jButMod;
    private javax.swing.JButton jButMult;
    private javax.swing.JButton jButOpenBrack;
    private javax.swing.JButton jButPlus;
    private javax.swing.JButton jButPower;
    private javax.swing.JButton jButResult;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextDouble;
    private javax.swing.JTextField jTextOperation;
    private javax.swing.JTextField jTextResult;
    private javax.swing.JTextField jTextTime;
    // End of variables declaration//GEN-END:variables
    
}
